package com.example.hotelback.controller;

import com.example.hotelback.service.FileUploadService;
import com.example.hotelback.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 测试文件上传
 */

@RestController
@RequestMapping("/photo")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    @ApiOperation("上传图片")
    @PostMapping("/upload")
    public Result upload(@RequestParam("id") int id, @RequestParam("file") MultipartFile photo, HttpServletRequest req) throws IOException { // 网络请求类（前端发过来一个req对象）
        fileUploadService.upload(id, photo, req);
        return Result.success();
    }

    @ApiOperation("获取图片")
    @GetMapping("/get")
    public Result getImage(String fileName, HttpServletResponse response) throws IOException {
        fileUploadService.getImage(fileName, response);
        return Result.success();
    }
}
